##use strict;
use HTML::TreeBuilder;
use File::Path;
use Time::Local 'timelocal';
#use warnings;
use File::Spec::Functions;
use Cwd;
use Win32::OLE;
use MIME::Lite;
use File::Copy; 
## Static variable Define##
my $MP_Filename ="China_Product_SW_Update_Schedule_MP.xlsx";
my $SD_Filename ="China_Product_SW_Update_Schedule_SD.xlsx";
my $currentdir= "C:\\FileChangeNotifyTool";
my $oldfilepath= "C:\\FileChangeNotifyTool\\Old_Files";
my $currentTestFilePath = "C:\\";
my $MP_Time_Stamp_path = $oldfilepath.'\\MP_Time_Stamp.txt';
my $SD_Time_Stamp_path = $oldfilepath.'\\SD_Time_Stamp.txt';
my $Old_MP_File_Path = $oldfilepath.'\\'.$MP_Filename;
my $Old_SD_File_Path = $oldfilepath.'\\'.$SD_Filename;
my $Correct_MP_File_Path = $currentTestFilePath.$MP_Filename;
my $Correct_SD_File_Path = $currentTestFilePath.$SD_Filename;
## End Static variable Define##

#************Main Scirpts*************#
print "Init Log File Tool Start...\n\n";

print "Start Check the MP File Init:\n";
##Get the Correct MP file modify time
my $MPModifyTime =&getModifyTime($Correct_MP_File_Path);
if($MPModifyTime ne ''){
		&insertTimeStamp($MP_Time_Stamp_path,$MPModifyTime);
		copy($Correct_MP_File_Path,$Old_MP_File_Path) or die "Copy failed: $!"; 
		print "SD File is update, Sending notification mail.\n";
} else {
	print "Cannot get the modify time.\n";
	&send_alert("F");
}



print "\n\nStart Check the SD File Init:\n";
##Get the Correct SD file modify time
my $SDModifyTime =&getModifyTime($Correct_SD_File_Path);
if($SDModifyTime ne ''){
		&insertTimeStamp($SD_Time_Stamp_path,$SDModifyTime);
		copy($Correct_SD_File_Path,$Old_SD_File_Path) or die "Copy failed: $!"; 
		print "SD File is updated, Sending notification mail.\n";
} else {
	print "Cannot get the modify time.\n";
}

if(($MPModifyTime ne '') &&($SDModifyTime ne '')){
	#&sendInitSuccess();
	&send_alert("P");
	print "Init Notification mail is sended.\n";
} else {
&send_alert("F");
print "Init Notification mail is sended.\n";
}

#************Main Scirpts End*************#


##Functions##########################################################
sub send_alert {
	($testresult)=@_;
	print "Start send alert Mail\n";
    my $to_address = 'ext-duan.ken@nokia.com';
    my $subject = "Result check Notification";
	my $Fulldata;
    my $Passdata =<<EOF;
<html>
<body bgcolor="green">
<h1>File Check Succeed!</h1>
</body>
</html>
EOF
    my $Faildata =<<EOF;
<html>
<body bgcolor="red">
<h1>File Check Fail!</h1>
</body>
</html>
EOF
if ($testresult eq "P"){
$Fulldata = $Passdata;
} else {
$Fulldata = $Faildata;
}

    my $msg = MIME::Lite->new (
        From => 'mape2emonitoring_SVC_BE@nokia.com',
        To => $to_address,
        Subject => $subject,
        Type     => 'text/html',
        Data     => $Fulldata,
        Encoding => 'base64',
    ) or die "create container failed: $!";

    $msg->attr('content-type.charset' => 'ANSI');
    $msg->send('smtp','smtp.nokia.com',Debug=>0);
	print "Mail sending is finished.\n";
}


sub sendErrorAlert{
	my $senderror = $currentdir.'\send_email_error.py';
	system($senderror);
	print "Error occurs. Send error message.\n\n";
}

sub sendInitSuccess{
	my $sendInit = $currentdir.'\send_email_init.py';
	system($sendInit);
	print "Init Success.\n\n";
}




sub sendModifyAlert{
	my ($mailSelection)=@_;
	if($mailSelection eq "MP"){
	my $sendalertsuccess = $currentdir.'\send_email2.py';
	system($sendalertsuccess);
	print "MP Modify alert is sent.\n\n";
	}
	if($mailSelection eq "SD"){
	my $sendalertsuccess = $currentdir.'\send_email.py';
	system($sendalertsuccess);
	print "SD Modify alert is sent.\n\n";
	}
}


sub insertTimeStamp{
	my($update_log_path,$timestamp)=@_;
	my $temppath='>'.$update_log_path;
	open(TS_FILE, $temppath) or die "Can't open $temppath.\n";
	print TS_FILE $timestamp;
	print TS_FILE "\n";
	close(TS_FILE);	
}

sub getModifyTime{
	my ($File_Path) = @_;
	my $L_file_modify_time ='';
	my @File_Attributes =stat($File_Path);
	if ($File_Attributes[9] eq ''){
		print "File Not found\n";
		##TODO Call warning mail
		&sendErrorAlert();
		return $L_file_modify_time;
		} else {
		$L_file_modify_time = $File_Attributes[9];
		print "File modify time stamp is $L_file_modify_time\n";
		return $L_file_modify_time;
		}
}

sub getLatestModifyTime{
	my($LogFilePath)=@_;
	open(LOG_FILE, $LogFilePath) or die "Can't open $LogFilePath\n";
	my $Last_Modify_Times ='';
	while(<LOG_FILE>)
		{
			chomp;
			#print "$_\n";
			$Last_Modify_Times = $_;
		}	
	close(LOG_FILE);
	print "Latest Log Modify file Time is $Last_Modify_Times\n";
	return $Last_Modify_Times;
}


##Functions##########################################################
